﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Controllers
{
    public class DoctorController : BaseController
    {
        Maticsoft.Model.T_Doctor M_user = new Maticsoft.Model.T_Doctor();
        Maticsoft.Model.T_History M_history = new Maticsoft.Model.T_History();
        Maticsoft.Model.T_Equipment M_equipment = new Maticsoft.Model.T_Equipment();
        Maticsoft.Model.T_User myUser1 = new Maticsoft.Model.T_User();
        Maticsoft.Model.T_User myUser2 = new Maticsoft.Model.T_User();
        Maticsoft.Model.T_User myUser3 = new Maticsoft.Model.T_User();
        string User_Select;

        #region 初始化
        public void GetUser()
        {
            try
            {
                Maticsoft.BLL.T_Doctor bll = new Maticsoft.BLL.T_Doctor();
                string id = Session["UserInfo"].ToString();
                M_user = bll.GetModel(id);
            }
            catch
            {
                Session["UserInfo"] = null;
            }
        }

        public Maticsoft.Model.T_User GetMyUser()
        {
            try
            {
                switch (User_Select)
                {
                    case "0": return myUser1; 
                    case "1": return myUser2; 
                    case "2": return myUser3; 
                }
            }
            catch
            {

            }
            return null;
        }

        public void SetViewData()
        {
            Maticsoft.BLL.T_Doctor bll = new Maticsoft.BLL.T_Doctor();
            Maticsoft.BLL.T_User bll_user = new Maticsoft.BLL.T_User();
            string id = Session["UserInfo"].ToString(); 
            User_Select =Session["UserSelect"].ToString();
            M_user = bll.GetModel(id);
            ViewData["UserName"] = M_user.Name;
            try
            {
                myUser1 = bll_user.GetModel(M_user.User1);  ViewData["User1"] = myUser1.Name;
                myUser2 = bll_user.GetModel(M_user.User2);  ViewData["User2"] = myUser2.Name;
                myUser3 = bll_user.GetModel(M_user.User3);  ViewData["User3"] = myUser3.Name;  
            }
            catch
            {
                
            }
            try
            {
                switch (User_Select)
                {
                    case "0": ViewData["UserTitle"] = ViewData["User1"]; break;
                    case "1": ViewData["UserTitle"] = ViewData["User2"]; break;
                    case "2": ViewData["UserTitle"] = ViewData["User3"]; break;
                    default: ViewData["UserTitle"] = ViewData["User1"]; break;
                }
            }
            catch
            {

            }
        }
        public void SetViewData(bool set)
        {

            Maticsoft.BLL.T_Doctor bll = new Maticsoft.BLL.T_Doctor();
            Maticsoft.BLL.T_User bll_user = new Maticsoft.BLL.T_User();
            Maticsoft.Model.T_User user = new Maticsoft.Model.T_User();
            string id = Session["UserInfo"].ToString();
            User_Select = Session["UserSelect"].ToString();
            M_user = bll.GetModel(id);
            ViewData["UserName"] = M_user.Name;
            if (!set)
            {
                ViewData["UserOld"] = M_user.Old;
                ViewData["UserOffice"] = M_user.Offices;
                ViewData["UserSex"] = M_user.Sex;
                ViewData["UserTel"] = M_user.Tel;
                ViewData["UserEmail"] = M_user.Email;
                ViewData["UserPassword"] = M_user.Password;
            }
            try
            {
                myUser1 = bll_user.GetModel(M_user.User1); ViewData["User1"] = myUser1.Name;
                if (!set) {ViewData["User1Id"] = myUser1.Id; ViewData["User1Tel"] = myUser1.Tel; ViewData["User1Email"] = myUser1.Email;}
                myUser2 = bll_user.GetModel(M_user.User2); ViewData["User2"] = myUser2.Name;
               if (!set) { ViewData["User2Id"] = myUser2.Id; ViewData["User2Tel"] = myUser2.Tel; ViewData["User2Email"] = myUser2.Email;}            
                myUser3 = bll_user.GetModel(M_user.User3); ViewData["User3"] = myUser3.Name;
                if (!set) { ViewData["User3Id"] = myUser3.Id; ViewData["User3Tel"] = myUser3.Tel; ViewData["User3Email"] = myUser3.Email;}
               
            }
            catch
            {

            }
            try
            {
                switch (User_Select)
                {
                    case "0": ViewData["UserTitle"] = ViewData["User1"];
                        SetHistory(myUser1); break;
                    case "1": ViewData["UserTitle"] = ViewData["User2"];
                        SetHistory(myUser2); break;
                    case "2": ViewData["UserTitle"] = ViewData["User3"];
                        SetHistory(myUser3); break;
                    default: ViewData["UserTitle"] = ViewData["User1"];
                        SetHistory(myUser1); break;
                }
            }
            catch
            {

            }

        }

        public void SetHistory(Maticsoft.Model.T_User user)
        {
            string[] offices = new string[4]{
                    "内科","外科","妇产科","儿科"
                };
            ViewData["UserName2"] = user.Name;
            ViewData["UserPassword"] = user.Password;
            ViewData["UserOffice"] = offices[user.Offices - 3];
            ViewData["UserId"] = user.Id;
            ViewData["UserIllness"] = user.Illness;
            ViewData["UserBed"] = user.Bed;
            ViewData["UserSex"] = user.Sex.Trim();
            ViewData["UserOld"] = user.Old;
            ViewData["UserEmail"] = user.Email;
            ViewData["UserTel"] = user.Tel;
            ViewData["UserPhoto"] = user.Photo;
            ViewData["UserHeight"] = user.Height;
            ViewData["UserWeight"] = user.Weight;
            ViewData["UserTime"] = user.BeHosptial;
            ViewData["UserDoctor"] = user.DoctorName;
            ViewData["DateTime"] = DateTime.Now.ToString("yyyy-MM-dd");
            Maticsoft.BLL.T_Doctor bll_doctor = new Maticsoft.BLL.T_Doctor();

            List<Maticsoft.Model.T_Doctor> doctor = (bll_doctor.GetModelList("Name='" + user.DoctorName.Trim() + "'"));
            if (doctor.Count > 0)
            {
                ViewData["UserDoctorTel"] = doctor[0].Tel;
                ViewData["UserDoctorEmail"] = doctor[0].Email;
            }

            M_history.UserName = user.Name;
            M_history.DoctorName = M_user.Name;

            if (InitData() == true)
            {
                Maticsoft.BLL.T_Monitor bll = new Maticsoft.BLL.T_Monitor();
                Maticsoft.Model.T_Monitor monitors = bll.GetModelList("Equipment='" + M_equipment.Id + "'")[0];
                M_history.Temp = monitors.Temp;
                M_history.Heart = monitors.Heart;
                M_history.Blood = monitors.Blood;
            }
        }

        public bool InitData()
        {
            Maticsoft.BLL.T_Equipment bll = new Maticsoft.BLL.T_Equipment();
            if (GetMyUser() != null)
            {
                List<Maticsoft.Model.T_Equipment> equipments = bll.GetModelList("UserId='" + GetMyUser().Id + "'");
                if (equipments.Count > 0)
                {
                    M_equipment = equipments[0];
                    return true;
                }
            }
            else
            {
                return false;
            }

            return false;
        }
        #endregion

        #region 设置选择
        public ActionResult Select()
        {
            string select = Request["id"];
            string chars = Request["chars"];
            if (!String.IsNullOrEmpty(chars))
            {
                Session["UserSelect"] = select;
                return Content("OK");
            } 
            else
            {

            }
            return Content("NO");
        }
        #endregion
        
        #region 首页
        public ActionResult Index()
        {
            SetViewData();
            return View();
        }
        #endregion

        #region  沟通
        public ActionResult Widgets()
        {
            SetViewData();
            return View();
        }
        #endregion

        #region 实时数据
        public ActionResult Charts()
        {
            SetViewData();
            return View();
        }
        #endregion

        #region 客户
        public ActionResult Tables()
        {
            SetViewData(); 
            return View();
        }
        #endregion

        #region 病历
        public ActionResult Forms()
        {
            SetViewData(true);
            return View();
        }
        #endregion

        #region 计划
        public ActionResult Panels()
        {
            SetViewData();
            return View();
        }
        #endregion

        #region 个人设置
        public ActionResult Person()
        {
            SetViewData(false);
            return View();
        }
        #endregion
      
        #region 获得谈话
        public JsonResult GetTalk()
        {
            SetViewData();
            if (M_user == null) {
                return null;
            }
            var res = new JsonResult();
            res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            Maticsoft.BLL.T_Talk bll = new Maticsoft.BLL.T_Talk();
            List<Maticsoft.Model.T_Talk> talks = bll.GetModelList("(DoctorName='" + M_user.Name + "')AND(UserName='"+GetMyUser().Name+"')");
            if (talks.Count > 0)
            {
                List<Maticsoft.Model.T_Talk> talk_list = new List<Maticsoft.Model.T_Talk>();
                if (talks.Count > 10)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        talk_list.Add(talks[talks.Count - i - 1]);
                    }
                }
                else
                {
                    for (int i = 0; i < talks.Count; i++)
                    {
                        talk_list.Add(talks[talks.Count - i - 1]);
                    }
                }
                res.Data = talk_list;
                return res;
            }
            else
            {
                return null;
            }
        }
        #endregion

        #region 添加谈话
         public ActionResult AddTalk()
        {
            string mess = Request["mess"];
            SetViewData();
            Maticsoft.Model.T_Talk talk = new Maticsoft.Model.T_Talk();
            Maticsoft.BLL.T_Talk bll = new Maticsoft.BLL.T_Talk();
            talk.DoctorName = M_user.Name;
            if( GetMyUser()!=null)
            {
                talk.UserName = GetMyUser().Name;
            }
            talk.Time = DateTime.Now;
            talk.Talker = M_user.Name;
            talk.Talk = mess;
            if (bll.Add(talk))
            {
                return Content("OK");
            }
            return Content("No");
        }

        #endregion

        #region 添加计划
          public ActionResult AddPlan()
        {
            string mess = Request["mess"];
            GetUser();
            Maticsoft.Model.T_Plan plan = new Maticsoft.Model.T_Plan();
            Maticsoft.BLL.T_Plan bll = new Maticsoft.BLL.T_Plan();
            plan.Name = M_user.Name;
            plan.PlanDate = DateTime.Now;
            plan.Plan = mess;
            plan.Del = false;
            if (bll.Add(plan))
            {
                return Content("OK");
            }
            return Content("No");
        }
         #endregion

        #region 获得计划
           public ActionResult GetPlan()
        {
            GetUser();
            var res = new JsonResult();
            res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            Maticsoft.BLL.T_Plan bll = new Maticsoft.BLL.T_Plan();
            List<Maticsoft.Model.T_Plan> talks = bll.GetModelList("DoctorName='" + M_user.Name + "'");
            if (talks.Count > 0)
            {
                List<Maticsoft.Model.T_Plan> talk_list = new List<Maticsoft.Model.T_Plan>();
                if (talks.Count > 10)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        talk_list.Add(talks[talks.Count - i]);
                    }
                }
                else
                {
                    for (int i = 0; i < talks.Count; i++)
                    {
                        talk_list.Add(talks[talks.Count - i - 1]);
                    }
                }
                res.Data = talk_list;
                return res;
            }
            else
            {
                return null;
            }
        }
          #endregion

        #region 获得数据
           public JsonResult Json()
           {
               var res = new JsonResult();
               SetViewData();
               if (InitData() == true)
               {
                   Maticsoft.BLL.T_Monitor bll = new Maticsoft.BLL.T_Monitor();
                   List<Maticsoft.Model.T_Monitor> monitors = bll.GetModelList("((Equipment='" + M_equipment.Id + "')AND(NowTime='*'))");
                   res.Data = monitors;
               }
               res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
               return res;
           }
         #endregion

        #region 获得病历
            public ActionResult GetHistory()
           {
               SetViewData();
               Maticsoft.Model.T_User myUser = GetMyUser();
               if (myUser == null)
               {
                   return null;
               }
               string history_str = "";
               Maticsoft.BLL.T_History bll = new Maticsoft.BLL.T_History();
               string datetime = Request["time"];
               List<Maticsoft.Model.T_History> historys = bll.GetModelList("(UserName='" + myUser.Name + "')AND((Time>='" + Request["time"] + " 00:00:00')AND(Time<='" + Request["time"] + " 23:59:59'))");
               if (historys.Count > 0)
               {
                   foreach (Maticsoft.Model.T_History history in historys)
                   {
                       history_str = history.Time.ToString("HH:mm") + "\r" + history.Chars + "\r";
                   }
               }
               return Content(history_str);
           }
           #endregion       

        #region 添加病历诊断
            public ActionResult AddHistory()
            {
                string history = Request["history"];
                SetViewData(true);
                M_history.Chars = history;
                Maticsoft.BLL.T_History bll = new Maticsoft.BLL.T_History();
                if (bll.Add(M_history))
                {
                    return Content("OK");
                }
               
                return Content("NO");
            }
            #endregion

        #region 修改个人质料
            public ActionResult SetDoctor()
            {
                Maticsoft.BLL.T_Doctor bll = new Maticsoft.BLL.T_Doctor();
                SetViewData();
                string name = Request["name"];
                string sex = Request["sex"];
                string id1 = Request["id1"];
                string id2 = Request["id2"];
                string id3 = Request["id3"];
                int old = int.Parse(Request["old"]);
                string tel = Request["tel"];
                string email = Request["email"];
                long offices = 6;
                string office = Request["offices"];
                switch (office)
                {
                    case "内科": offices = 3; break;
                    case "外科": offices = 4; break;
                    case "妇产科": offices = 5; break;
                    default
                    : break;
                }
                M_user.Name = name;
                M_user.Email = email;
                M_user.Old = old;
                M_user.Sex = sex;
                M_user.Tel = tel;
                M_user.Offices = offices;
                M_user.User1 = id1;
                M_user.User2 = id2;
                M_user.User3 = id3;
                if (bll.Update(M_user))
                {
                    return Content("OK");
                }
                return Content("NO");
            }
            #endregion
            
    }
}
